package com.org.khtn.ultilities;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.org.khtn.command.select.Where;
import com.org.khtn.data.DataTables;
import com.org.khtn.data.Table;

public class Ultilities {
	final static String className = "Ultilities:";

	/**
	 * Loc du lieu cua table co dieu kien cho truoc
	 * 
	 * @param myTable
	 * @param condition
	 * @return
	 */
	public static Vector filterByWhereCondition(Table myTable,
			String condition, DataTables dt) {
		final String methodName = "filterByWhereCondition():";
		Map<String, Map<String, String>> data = new HashMap<String, Map<String, String>>();
		data = myTable.getData();
		Map<String, Multimap<String, String>> resultData = new HashMap<String, Multimap<String, String>>();
		resultData = Ultilities.mapToMultimap(myTable.getData());

		Where where = new Where();
		where.putData(resultData);
		where.setDatabaseName(dt.getName());
		where.setTables(new String[] { myTable.getName() });
		where.putCurrentTables(myTable.getName());
		Vector v = dt.getAllTableName();
		System.out.println(className + methodName + "v:" + v);
		int length = v.size();
		System.out.println(className + methodName + "length:" + length);

		String[] tam123 = new String[length];

		for (int i = 0; i < length; i++) {
			tam123[i] = (String) v.get(i);
			where.putTableDefine(tam123[i], dt.getTableDefines(tam123[i]));
			System.out.println(className + methodName + "tam123:" + tam123[i]);

		}
		System.out.println();
		Vector rowID = new Vector<>();
		rowID = where.where(condition).getAllResultDataRowID();
		return rowID;
	}

	/**
	 * Xuất 1 mảng object
	 * 
	 * @param ob
	 */
	public static void print_r(Object[] ob) {
		int length = ob.length;
		System.out.print(ob.getClass().getSimpleName() + ":");
		for (int i = 0; i < length; i++) {
			System.out.print(ob[i] + ":");
		}
		System.out.println();
	}

	/**
	 * copy 1 file từ sourcepath đến đích
	 * 
	 * @param sourcePath
	 * @param destPath
	 * @throws IOException
	 */
	public static void CopyFile(String sourcePath, String destPath)
			throws IOException {
		File source = new File(sourcePath);
		File dest = new File(destPath);

		InputStream fin = new FileInputStream(source);
		fin = new BufferedInputStream(fin);
		try {
			OutputStream fout = new FileOutputStream(dest);
			fout = new BufferedOutputStream(fout);
			try {
				int c;
				while ((c = fin.read()) >= 0) {
					fout.write(c);
				}

			} finally {
				fout.close();
			}
		} finally {
			fin.close();
		}

	}

	/**
	 * kiểm tra sự tồn tại 1 field trong table
	 * 
	 * @param name
	 * @param myData
	 * @return
	 */
	public static Boolean fieldExist(String name,
			Map<String, Multimap<String, String>> myData) {
		Multimap<String, String> record0 = myData.get("0");
		if (record0.containsKey(name))
			return true;
		else
			return false;
	}

	/**
	 * nhan 2 table
	 * 
	 * @param table1
	 * @param table2
	 * @return
	 */
	public static Map<String, Multimap<String, String>> mul(
			Map<String, Multimap<String, String>> table1,
			Map<String, Multimap<String, String>> table2) {
		final String methodName = "mul():";
		System.out.println(className + methodName + "table1:" + table1);
		System.out.println(className + methodName + "table2:" + table2);

		if (table1 == null || table2 == null) {
			return null;
		}
		Map<String, Multimap<String, String>> result = new HashMap<String, Multimap<String, String>>();
		int length1 = table1.size();
		int length2 = table2.size();
		for (int i = 0; i < length1; i++) {
			System.out.println(className + methodName + "i:" + i);
			Multimap<String, String> rowTableA = ArrayListMultimap.create();
			rowTableA.clear();
			rowTableA.putAll(table1.get("" + i));
			for (int j = 0; j < length2; j++)

			{
				Multimap<String, String> row = ArrayListMultimap.create();
				System.out.println(className + methodName + "row num:"
						+ (i * 2 + j));
				row.putAll(rowTableA);
				System.out.println(className + methodName + "row 1:" + row);

				row.putAll(table2.get(j + ""));
				System.out.println(className + methodName + "row 2:" + row);
				result.put((i * 2 + j) + "", row);
			}

		}
		return result;

	}

	/**
	 * chuyển từ map sang multimap
	 * 
	 * @param data
	 * @return
	 */
	public static Map<String, Multimap<String, String>> mapToMultimap(
			Map<String, Map<String, String>> data) {

		Map<String, Multimap<String, String>> finalResult = new HashMap<String, Multimap<String, String>>();

		for (Map.Entry<String, Map<String, String>> e : data.entrySet()) {

			Multimap<String, String> tam = ArrayListMultimap.create();
			for (Map.Entry<String, String> e1 : e.getValue().entrySet()) {
				tam.put(e1.getKey(), e1.getValue());
			}
			finalResult.put(e.getKey(), tam);
		}
		return finalResult;

	}
	public static Map<String,Multimap<String,String>> createError(String error)
	{
		Map<String,Multimap<String,String>> result =  new HashMap<String, Multimap<String,String>>();
		Multimap<String,String> tam =ArrayListMultimap.create();
		tam.put("error", error);
		result.put("error", tam);
		return result;
		
	}

	/**
	 * Xóa file
	 * 
	 * @param sourcePath
	 */

	public static void DeleteFile(String sourcePath) {
		File source = new File(sourcePath);
		if (source.exists()) {
			source.delete();
		}

	}
}
